package org.skydiveapp;

import org.skydiveapplibrary.data.FreefallProfileType;
import org.skydiveapplibrary.ui.FreefallProfileTypeSelectListAdapter;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class FreefallProfileTypeSelectListActivity extends BaseListActivity
{	
    @Override
    public void onCreate(Bundle savedInstanceState)
    { 
    	super.onCreate(savedInstanceState);
    	
    	// init view
    	getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    	setHeaderText(R.string.select_freefall_profile_type_label);
    }
    
    @Override
    public void onResume()
    {
    	super.onResume();
    	
    	// init listview
    	FreefallProfileTypeSelectListAdapter adapter = new FreefallProfileTypeSelectListAdapter(this);
		setListAdapter(adapter);
		
		// set initial selection
		FreefallProfileType selectedType = getCurrentSelection();
		for (int index = 0; index < adapter.getCount(); index++)
		{
			getListView().setItemChecked(index,
					adapter.getItems().get(index) == selectedType); 
		}
    }
    
    @Override
    protected void onListItemClick(ListView listView, View view, int indx, long id)
    {
    	// get current selection
    	FreefallProfileType selectedType = getCurrentSelection();
    	// get new selection
    	FreefallProfileTypeSelectListAdapter adapter = (FreefallProfileTypeSelectListAdapter)getListAdapter();
		FreefallProfileType newSelectedType = (FreefallProfileType)adapter.getItem(indx);
    	
    	// create new intent
    	Intent intent =  new Intent();
    	
    	// if different than previous
    	if (selectedType != newSelectedType)
    	{
    		intent.putExtra(Constants.EXTRA_FREEFALL_PROFILE_TYPE, newSelectedType.toString());
    	}

    	// set result, finish
		setResult(RESULT_OK, intent);
		finish();
    }
    
    private FreefallProfileType getCurrentSelection()
    {
    	String typeStr = getIntent().getStringExtra(Constants.EXTRA_FREEFALL_PROFILE_TYPE);
		return (typeStr != null) ? FreefallProfileType.valueOf(typeStr) : null;
    }
} 